//
// Created by jinxd on 19-2-18.
//

#ifndef ONENET_TRACING98_LIGHTSTEPTRACEROPTIONS_H
#define ONENET_TRACING98_LIGHTSTEPTRACEROPTIONS_H

#include <string>
#include <vector>
#include <unordered_map>
#include <any>
#include <functional>
#include <cstring>

// DynamicConfigurationValue is used for configuration values that can
// be either fixed or changed at runtime. To specify a fixed value, just assign
// a constant
//    DynamicConfigurationValue<int> value = 123;
// for a dynamic value, use a functor
//    DynamicConfigurationValue<int> value = [] {
//        int value = /* look up dynamically */
//        return value;
//     };
template<class T>
class DynamicConfigurationValue
{
public:
	explicit DynamicConfigurationValue(std::function<T()> value_functor)
			: value_functor_{std::move(value_functor)}
	{}

	explicit DynamicConfigurationValue(const T &value)
	{
		value_functor_ = [value]
		{ return value; };
	}

	T value() const
	{ return value_functor_(); }

private:
	std::function<T()> value_functor_;
};


struct TracerOptions
{
	/**
	 * 服务名称
	 */
	std::string service_name;

	/**
	 * 采样数据Collector地址（ip+port)
	 */
	std::vector<std::string> kafka_collector_host;

	/**
	 *	key-value形式数据，由Tracer创建的Span均会携带此tag数据
	 */
	std::unordered_map<std::string, std::any> tags;

	// Set `verbose` to true to enable more text logging.
	bool verbose = false;

	/**
	 * 设置Span最大缓冲数量(可通过环境变量动态调整缓冲数量)
	 */
	DynamicConfigurationValue<int> max_buffered_spans = DynamicConfigurationValue(getMaxBufferedSpansCount());

private:

	int getMaxBufferedSpansCount()
	{
		const char *countStr = std::getenv("MAX_BUFFERED_SPANS");
		if (nullptr != countStr)
		{
			char *end = const_cast<char *>(countStr + std::strlen(countStr));
			int count = static_cast<int>(std::strtol(countStr, &end, 10));
			if (0 == errno) return count;
		}
		return 2000;
	}
};

#endif //ONENET_TRACING98_LIGHTSTEPTRACEROPTIONS_H
